// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-10-23 16:03:15
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 14:27:24
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Constraint/MPCParser.cpp
 */

#include "Import/BDFImport/BulkData/Constraint/MPCParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IMPCService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (MPC);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/MPC/MPC.md>
*/

enum MPCIndex : std::uint8_t
{
    MPCIndex_SID = 1,
    MPCIndex_G1  = 2,  // Identification number of grid or scalar point. (Integer > 0)
    MPCIndex_C1  = 3,  // Component number. (Any one of the Integers 1 through 6 for grid points; blank, zero or 1 for scalar points.)
    MPCIndex_A1  = 4,  // Coefficient. (Real; Default = 0.0 except A1 must be nonzero.)
    MPCIndex_G2  = 5,
    MPCIndex_C2  = 6,
    MPCIndex_A2  = 7,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseMPC (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                           std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::MPCData tempData;

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    // 解析字符串

    tempData.m_sId = convertToI64 (dividedEntryInfo[MPCIndex_SID]);
    // 可能一行处理两个MPCComponent
    SG::DataStructure::FEM::MPCData::MPCComponent mpcComponent1;  // 记录一行中前一个MPCComponent
    SG::DataStructure::FEM::MPCData::MPCComponent mpcComponent2;  // 记录一行中后一个MPCComponent

    // 计算分割后的行数，每 10 个字段为一行
    const size_t rowSize = dividedEntryInfo.size () / 10;

    tempData.m_masterItems.reserve (2 * rowSize);
    // 遍历每一行的条目
    for (size_t curRow = 0; curRow < rowSize; curRow++)
    {
        mpcComponent1.m_nodeId = mpcComponent2.m_nodeId = 0;
        // 遍历每行的第 2 到第 9 个字段（不包含首字段）
        for (size_t fieldsColumn = 1; fieldsColumn < 8; fieldsColumn++)
        {
            // 计算当前字段的索引
            const size_t curIndex  = curRow * 10 + fieldsColumn;
            std::string& curStrKey = dividedEntryInfo[curIndex];
            // 检查当前字段是否为空白
            bool const isEmpty = is_all_space (curStrKey);
            if (isEmpty)
            {
                continue;
            }

                switch (fieldsColumn)
            {
            case MPCIndex_G1:
                mpcComponent1.m_nodeId = convertToI64 (curStrKey);
                break;
            case MPCIndex_C1:
                mpcComponent1.m_dof = convertToI64 (curStrKey);
                break;
            case MPCIndex_A1:
                mpcComponent1.m_factor = convertToReal (curStrKey);
                break;
            case MPCIndex_G2:
                mpcComponent2.m_nodeId = convertToI64 (curStrKey);
                break;
            case MPCIndex_C2:
                mpcComponent2.m_dof = convertToI64 (curStrKey);
                break;
            case MPCIndex_A2:
                mpcComponent2.m_factor = convertToReal (curStrKey);
                break;
            default:
                break;
            }
        }
        // 序列中的第一个自由度（G1，C1）定义为从自由度。
        if (0 == curRow)
        {
            tempData.m_slaveItem = mpcComponent1;
        }
        // 非第一个的自由度（G2，C2）定义到m_masterItems
        else
        {
            tempData.m_masterItems.push_back (mpcComponent1);
        }
        // 通过nodeID是否为0判断这一行是否存在第二个mpcComponent2
        if (mpcComponent2.m_nodeId != 0)
        {
            tempData.m_masterItems.push_back (mpcComponent2);
        }
    }
    // 添加到数据库中
    return dbServiceFactory->get<IMPCService> ()->Add (tempData);
}
